package aaaCfg;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

//import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.engine.spi.SessionFactoryImplementor;
//import org.hibernate.jdbc.Work;
//import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
//import org.springframework.orm.hibernate4.SessionFactoryUtils;

import java.util.Properties;

//import org.hibernate.Session;
//import org.hibernate.SessionFactory;










  




import com.attilax.Closure;
import com.attilax.core;
import com.attilax.db.DBX;
//import com.attilax.db.DbServiceQ5;
import com.attilax.db.DbServiceV3Q68;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.Closure2;
import com.attilax.lang.Trigger;
import com.attilax.net.HttpRequest;
import com.attilax.net.websitex;
import com.attilax.persistence.DBCfg;
 
import com.attilax.persistence.DbutilX;
//import com.attilax.db.Hb4JdbcX;
//import com.attilax.lang.Closure2;
import com.attilax.persistence.Hbx;
import com.attilax.persistence.HbxX;
import com.attilax.persistence.PX;
import com.attilax.spri.SpringUtil;
import com.attilax.util.HibernateSessionFactory;
import com.attilax.util.PropX;
//import com.focustar.entity.TMbWeixinuser;
import com.focustar.util.BaseImpl;
//import com.focusx.dao.BranchManagerDao;
//import com.focusx.dao.impl.BranchManagerDaoImpl;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.name.Names;

/**
 * aaaCfg.Ioc4game
 * @author attilax
 *2016年6月6日 下午4:22:54
 */
public class Ioc4game {
	static Injector ati;
	public static Map  cfgMap=new HashMap ();
	public static Map<String, Object> map = new HashMap();
	public static String cfgPath;
	public static boolean isInied=false;
	{
		ini();
	}
	public static 	String dbcfg_file;
	public static void ini() {
		dbcfg_file = pathx
				.webAppPath()
				+ "/"+System.getProperty("cfgfile");
		map.put("copyright-trigger", new Trigger() {

			@Override
			public Object exec(Object object) {
				String fildId = (String) object;
				String url = "http://120.24.61.254/efilm/a/film/";
				url = url
						+ "filmMaster/submitOrder?totalPrice=200&filmId=1&houseId=2&ShopId=23&key=keikdunkdfydfhhkdk343rere";
				System.out.println(url);
				String[] split = url.split("\\?");
				String param = split[1];
				String urlOnly = split[0];
				String r = HttpRequest.sendPost(urlOnly, param);
				System.out.println(r);
				return r;
			}

		});
		 


	}

	protected static String getBranchID(String cfgPath) {
		
		String url_inner = PropX.getConfig(cfgPath, "branch-id");
		// String playserver = PropX.getConfig(cfgPath, "playserver");
		if (url_inner.contains("%app-path%"))
			url_inner = url_inner.replaceAll("%app-path%",
					PropX.getConfig(cfgPath, "app-path"));
		String authcode_inner = websitex.WebpageContent(url_inner);
		return authcode_inner;
	}

	public static void main(String[] args) {
		
		System.setProperty("prj", "game");
		System.setProperty("cfgfile", "cfg_game.properties");
		System.setProperty("ioccfg", "aaaCfg.Ioc4game");
		DBX dx = Ioc4game.getBean(DbServiceV3Q68.class);
		System.out.println(core.toJsonStrO88(dx
				.execSql("select * from timer_tab limit 5 ")));
	}

	/**
	 * @author attilax 老哇的爪子
	 * @since o92 j_48_40
	 * 
	 * @param class1
	 * @return
	 */
	public static <t> t getBean(Class<t> class1) {
		
		if(!isInied)
			ini();
		// attilax 老哇的爪子 j_48_40 o92
		if (ati == null) // ini singleon patter..
		{
			ati = Guice.createInjector(new Module() {

				@Override
				public void configure(Binder binder) {

					AnnotatedBindingBuilder<DBCfg> bind = binder.bind(DBCfg.class);
					bind.toInstance(
							getDbcfg().execute(null));
					binder.bind(DBX.class).to(DbServiceV3Q68.class);
					binder.bind(Connection.class).to(ConnectionImp.class);
					binder.bindConstant().annotatedWith(Names.named("thql"))
							.to(" from TUserUsers ");
				}

				private Closure2<Object, DBCfg> getDbcfg() {
					return new Closure2<Object, DBCfg>() {

						@Override
						public DBCfg execute(Object arg0) {
							DBCfg cfg = new DBCfg();
							try {
								// propertyReader pro=new Properties();
							
								FileInputStream fis = new FileInputStream(
										new File(dbcfg_file));
								Properties properties = new Properties();
								properties.load(fis);
								// pro.
								String url = properties
										.getProperty("jdbc.url");
								String driver = properties
										.getProperty("jdbc.driverClassName");

								String uname = properties
										.getProperty("jdbc.username");
								String pwd = properties
										.getProperty("jdbc.password");
						 
								cfg.setUrl(url);
								cfg.setUser(uname);
								cfg.setPassword(pwd);
								cfg.setDriver(driver);
								return cfg;
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								throw new RuntimeException(
										"cant ini dbcfg", e);
							}

						}
					};
				}
			});
		}

		//Injector[bindings=[ProviderInstanceBinding[key=Key[type=com.google.inject.Injector, annotation=[none]], 
		//source=[unknown source], scope=Scopes.NO_SCOPE, provider=Provider<Injector>], 
		//ProviderInstanceBinding[key=Key[type=java.util.logging.Logger, annotation=[none]], source=[unknown source], scope=Scopes.NO_SCOPE, provider=Provider<Logger>], //
		//InstanceBinding[key=Key[type=com.google.inject.Stage, annotation=[none]], source=[unknown source], instance=DEVELOPMENT], //
		//InstanceBinding[key=Key[type=com.attilax.persistence.DBCfg, annotation=[none]], source=aaaCfg.Ioc4game$2.configure(Ioc4game.java:147), 
		//instance=com.attilax.persistence.DBCfg@5e316c74], LinkedKeyBinding[key=Key[type=com.attilax.db.DBX, annotation=[none]], source=aaaCfg.Ioc4game$2.configure(Ioc4game.java:150), scope=Scopes.NO_SCOPE, target=Key[type=com.attilax.db.DbServiceV3Q68, annotation=[none]]], InstanceBinding[key=Key[type=java.lang.String, annotation=@com.google.inject.name.Named(value=thql)], source=aaaCfg.Ioc4game$2.configure(Ioc4game.java:152), instance= from TUserUsers ]]]
		return ati.getInstance(class1);

	}

}
